package ipgather.core.host.thread;

import org.apache.http.impl.client.CloseableHttpClient;

import ipgather.constant.PortConstant;
import ipgather.core.proxy.thread.ProxyCheck;
import ipgather.core.redis.RedisDbAdapter;
import ipgather.entity.Host;
import ipgather.util.HostUtil;

public class HostThread extends Thread{
	private String host;
	private CloseableHttpClient httpClient;
	public HostThread(final String host,CloseableHttpClient httpClient) {
		this.host = host;
		this.httpClient = httpClient;
	}

	public void run() {
		for (int port : PortConstant.PORTS) {//搜索常用服务器http端口
			execute(host, port);
		}
	}

	private void execute(final String host,final int port) {
		if (HostUtil.checkCollect(host, port)) {
			if(new ProxyCheck(httpClient).checkProxyHost(new Host(host, port))){
				System.err.println(new StringBuffer("有效[ ").append(host).append(" : ").append(port).append(" ]").toString());
				RedisDbAdapter.create().saveHost(host, port);// 保存到redis数据库
			}
		}
	}

}
